package com.geek.di;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class BeanDefinition {
    private String id;
    private String className;
    private List<ConstructorArg> constructorArgs = new ArrayList<>();
    private Scope scope = Scope.SINGLETON;
    private boolean lazyInit = false; // 省略必要的getter/setter/constructors

    public boolean isSingleton() {
        return scope.equals(Scope.SINGLETON);
    }

    public static enum Scope {SINGLETON, PROTOTYPE}

    @Data
    public static class ConstructorArg {
        private boolean isRef;
        private Class<?> type;
        private Object arg; // 省略必要的getter/setter/constructors

        public ConstructorArg(Builder builder) {
            this.isRef = builder.isRef;
            this.type = builder.type;
            this.arg = builder.arg;
        }

        public static class Builder {
            private boolean isRef;
            private Class<?> type;
            private Object arg;

            public ConstructorArg build() {
                if (this.isRef) {
                    if (this.type != null) {
                        throw new IllegalArgumentException("参数为引用类型时，无需设置type值");
                    }
                    if (arg == null || !(arg instanceof String)) {
                        throw new IllegalArgumentException("请设置引用ID");
                    }
                } else {
                    if (this.type == null || this.arg == null) {
                        throw new IllegalArgumentException("当参数为非引用类型时，type 和 arg 参数必填");
                    }
                }
                return new ConstructorArg(this);
            }

            public Builder setRef(boolean f) {
                this.isRef = f;
                return this;
            }

            public Builder setType(Class<?> type) {
                this.type = type;
                return this;
            }

            public Builder setArg(Object arg) {
                this.arg = arg;
                return this;
            }
        }
    }
}